package com.example.api.service.impl;

import static org.hamcrest.CoreMatchers.nullValue;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.api.dao.DatabaseInfoMapper;
import com.example.api.entity.DatabaseInfo;
import com.example.api.entity.TableInfo;
import com.example.api.entity.User;
import com.example.api.service.DatabaseInfoService;
import com.example.myutil.JDBCTools;

import lombok.experimental.var;
@Service
public class DataBaseInfoServiceImpl implements DatabaseInfoService {

	@Autowired
	private DatabaseInfoMapper dataBaseInfoMapper;
	
	/**
	 * 获取数据库连接信息下拉列表
	 */
	@Override
	public List<DatabaseInfo> getDatabaseInfoByuserId(Integer userId) {
		List<DatabaseInfo> list = dataBaseInfoMapper.getDatabaseConnectionInfoByuserId(userId);
		return list;
	}
	
	/**
	 * 获取左边数据库的展示列表
	 */
	@Override
	public List<Map<String, String>> getDatabaseInfoByConnectionId(Integer databaseConnectionId) {
		DatabaseInfo databaseInfo = dataBaseInfoMapper.getDatabaseConnectionInfoByConnectionId(databaseConnectionId);
		//List<Map<String, String>> getDatabaseList(String databasetype, String ip, String port,String username, String password)
		List<Map<String, String>> databaseList = JDBCTools.getDatabaseList(databaseInfo.getDatabaseType(),databaseInfo.getIp(),databaseInfo.getPort(),databaseInfo.getDatabaseUserName(),databaseInfo.getDatabasePassword());
		
		
//		int length=databaseInfoList.size();
//		for(int i = 0; i<length;i++) {
//			DatabaseInfo databaseInfo=databaseInfoList.get(i);
//			DatabaseInfo databaseInfo2=new DatabaseInfo();
//			databaseInfo2.setId(-databaseInfo.getId());
//			databaseInfo2.setDatabaseName("表");
//			databaseInfo2.setDatabaseType("table");
//			databaseInfo2.set_parentId(databaseInfo.getId());
//			databaseInfo2.setState("closed");
//			databaseInfoList.add(databaseInfo2);
//			DatabaseInfo databaseInfo3=new DatabaseInfo();
//			databaseInfo3.setId(-2147483648+databaseInfo.getId());
//			databaseInfo3.setDatabaseName("视图");
//			databaseInfo3.setDatabaseType("view");
//			databaseInfo3.setState("closed");
//			databaseInfo3.set_parentId(databaseInfo.getId());
//			databaseInfoList.add(databaseInfo3);
//		}
		
		return databaseList;
	}
	
	
	@Override
	public List<Map<String, Object>> getTableInfoBydatabaseInfo(String databaseName,Integer connectionId) {
		DatabaseInfo baseInfo = dataBaseInfoMapper.getDatabaseConnectionInfoByConnectionId(connectionId);
		List<Map<String, Object>> result=JDBCTools.listTables(baseInfo.getDatabaseType(),
				baseInfo.getIp(), baseInfo.getPort(),databaseName,
				baseInfo.getDatabaseUserName(),baseInfo.getDatabasePassword());
		return result;
	}
	
	/**
	 * 获取数据库表的字段信息
	 */
	public Map<String, Object> getTableFieldList(String databaseName, Integer connectionId,String tableName) {
		DatabaseInfo baseInfo = dataBaseInfoMapper.getDatabaseConnectionInfoByConnectionId(connectionId);
		Map<String, Object> map=new HashMap<>();
		List<Map<String, Object>> list = JDBCTools.listColumns(baseInfo.getDatabaseType(),baseInfo.getIp(),
				baseInfo.getPort(),databaseName,
				baseInfo.getDatabaseUserName(),baseInfo.getDatabasePassword(),tableName);
		map.put("total", 10);
		map.put("rows", list);
		map.put("footer", null);
		return map;
	}
	
	/**
	 * 获取数据配置信息
	 */
	@Override
	public List<DatabaseInfo> getDatabaseConfigInfoByuserId(Integer userId) {
		List<DatabaseInfo> list=dataBaseInfoMapper.getDatabaseConnectionInfoByuserId(userId);
		return list;
	}

	@Override
	public int addDatabaseConfigInfo(DatabaseInfo databaseInfo) throws Exception {
		int count=dataBaseInfoMapper.addDatabaseConfigInfo(databaseInfo);
		return count;
	}

	@Override
	public int deleteDatabaseConfigInfo(Integer connectionId) throws Exception {
		int count=dataBaseInfoMapper.deteleDatabaseConfigInfo(connectionId);
		return count;
	}

	@Override
	public User getuserInfoById(Integer userId) {
		User user = dataBaseInfoMapper.getuserInfoById(userId);
		return user;
	}

	@Override
	public User getUserInfoByLoginInfo(String userName, String password) {
		User user = dataBaseInfoMapper.getUserInfoByLoginInfo(userName, password);
		return user;
	}

	@Override
	public List<Map<String, Object>> getViewListBydatabaseInfo(String databaseName, Integer connectionId) {
		DatabaseInfo baseInfo = dataBaseInfoMapper.getDatabaseConnectionInfoByConnectionId(connectionId);
		List<Map<String, Object>> result=JDBCTools.listViews(baseInfo.getDatabaseType(),
				baseInfo.getIp(), baseInfo.getPort(),databaseName,
				baseInfo.getDatabaseUserName(),baseInfo.getDatabasePassword());
		return result;
	}

	@Override
	public Map<String, Object> getViewFieldList(String databaseName, Integer connectionId, String tableName) {
		DatabaseInfo baseInfo = dataBaseInfoMapper.getDatabaseConnectionInfoByConnectionId(connectionId);
		Map<String, Object> map=new HashMap<>();
		List<Map<String, Object>> list = JDBCTools.viewStructure(baseInfo.getDatabaseType(),baseInfo.getIp(),
				baseInfo.getPort(),databaseName,
				baseInfo.getDatabaseUserName(),baseInfo.getDatabasePassword(),tableName);
		map.put("total", 10);
		map.put("rows", list);
		map.put("footer", null);
		return map;
	}

}
